﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Infrastructure.Model.DI;
using Model.CommonModel.PageModel;
using Model.EntityModel.Auth;
using Model.QueryModel.system;
using Repository.IRepository.IAuthRepository;
using Repository.IRepository.ISysRepository;

namespace Repository.Repository.AuthRepository
{
    [AppService(ServiceType = typeof(IMenuButtonsRepository), ServiceLifetime = LifeTime.Transient)]
    public class MenuButtonsRepository : SqlSugarBaseRepository<MenuButtons>, IMenuButtonsRepository
    {
        // 单条新增
        public bool Insert(MenuButtons entity)
        {
            return _db.Insertable(entity).ExecuteCommand() > 0;
        }

        // 批量新增
        public bool InsertRange(List<MenuButtons> entities)
        {
            return _db.Insertable(entities).ExecuteCommand() > 0;
        }

        // 单条更新
        public bool Update(MenuButtons entity)
        {
            return _db.Updateable(entity).ExecuteCommand() > 0;
        }

        // 批量更新
        public bool UpdateRange(List<MenuButtons> entities)
        {
            return _db.Updateable(entities).ExecuteCommand() > 0;
        }

        // 单条删除（根据ID）
        public bool Delete(string id)
        {
            return _db.Deleteable<MenuButtons>().Where(m => m.Id == id).ExecuteCommand() > 0;
        }

        // 批量删除（根据ID集合）
        public bool DeleteRange(List<string> ids)
        {
            return _db.Deleteable<MenuButtons>().Where(m => ids.Contains(m.Id)).ExecuteCommand() > 0;
        }

        // 根据ID查询单条
        public MenuButtons Get(string id)
        {
            return _db.Queryable<MenuButtons>().Where(m => m.Id == id).First();
        }

        // 根据菜单ID查询按钮集合
        public List<MenuButtons> GetByMenuId(long menuId)
        {
            return _db.Queryable<MenuButtons>().Where(m => m.MenuId == menuId).ToList();
        }

        // 查询所有记录
        public List<MenuButtons> GetAll()
        {
            return _db.Queryable<MenuButtons>().ToList();
        }

        // 带条件的分页查询
        public PagedInfo<MenuButtons> GetPageList(int pageNum, int pageSize, MenuButtonsQuery search)
        {
            int totalCount = 0;
            var queryable = _db.Queryable<MenuButtons>()
                .WhereIF(!string.IsNullOrWhiteSpace(search.Id), x => x.Id.Contains(search.Id))
                .WhereIF(search.MenuId != null, x => x.MenuId == search.MenuId);
            // 执行分页查询
            var pages = queryable.ToPageList(pageNum, pageSize, ref totalCount);
            var totalPages = (totalCount - 1) / pageSize + 1;

            return new PagedInfo<MenuButtons>
            {
                PageIndex = pageNum,
                PageSize = pageSize,
                TotalCount = totalCount,
                Data = pages,
                TotalPage = totalPages
            };
        }
       
    }

    // 分页模型辅助类
   
}
